#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_INIT([corset], [1.04], [<nadia.davidson@mcri.edu.au>])

CXXFLAGS="-O3 -ffast-math -I./ $CXXFLAGS $CPPFLAGS"

AC_PROG_CXX()
AC_LANG(C++)
AC_PROG_MAKE_SET

AC_ARG_WITH(bam-lib, [  --with-bam_lib
                  specify the directory where the samtools libraries can be found])
AC_ARG_WITH(bam-inc, [  --with-bam_inc
                  specify the directory where the samtools headers can be found])
AC_ARG_WITH(htslib-dir, [  --with-htslib
                  specify the directory where the htslib headers can be found (default is <samtools directory>/htslib-1.0) ])

SAMTOOLS_INC_DIR=$with_bam_inc
if test "$SAMTOOLS_INC_DIR" != ""
then
        CXXFLAGS="-I$SAMTOOLS_INC_DIR $CXXFLAGS"
fi

HTSLIB_DIR=`echo $SAMTOOLS_INC_DIR/htslib-*.*` 
if test "$with_htslib" != ""
then
	HTSLIB_DIR=$with_htslib
fi
#fix because the static library doens't play nicely in mac OS X.
AC_CHECK_FILE( [$HTSLIB_DIR/libhts.a], [
   LIBS="$LIBS $HTSLIB_DIR/libhts.a"  &&
   CXXFLAGS="-I$HTSLIB_DIR $CXXFLAGS" ] )

SAMTOOLS_LIB_DIR=$SAMTOOLS_INC_DIR
if test "$with_bam_lib" != ""
then
	SAMTOOLS_LIB_DIR=$with_bam_lib
fi
LDFLAGS="-L$SAMTOOLS_LIB_DIR $LDFLAGS"
LD_RUN_PATH="$LD_RUN_PATH:$SAMTOOLS_LIB_DIR"

#Checks for samtools libraries and header files
AC_CHECK_HEADERS([bam.h sam.h], [], [
  echo "" &&
  echo "ERROR: Could not find the samtools headers." &&
  echo "       Please specify the location with: " &&
  echo "       './configure --with-bam_inc=<directory containing sam.h, bam.h etc.> " &&
  exit -1
])

AC_CHECK_LIB([z], [main], [], [
  echo "" &&
  echo "ERROR: Could not find the samtools library, libbam." &&
  echo "       Please specify the location with: " &&
  echo "       './configure --with-bam_lib=<directory containing bam library> " &&
  exit -1
])

AC_CHECK_LIB([bam], [main], [], [
  echo "" &&
  echo "ERROR: Could not find the samtools library, libbam." &&
  echo "       Please specify the location with: " &&
  echo "       './configure --with-bam_lib=<directory containing bam library> " &&
  exit -1
])


AC_CHECK_LIB([pthread], [main], [], [])


AC_CONFIG_FILES([Makefile])

my_save_cflags="$CXXFLAGS"
CXXFLAGS=-std=c++0x
AC_MSG_CHECKING([whether compiler supports -std=c++0x])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([])],[
	my_save_cflags="$my_save_cflags -std=c++0x -DUNORDEREDMAP"],
	[AC_MSG_RESULT([no])]
)
CXXFLAGS="$my_save_cflags"

AC_SUBST(LD_RUN_PATH,$LD_RUN_PATH)

AC_OUTPUT
